<?php
/**
 * 考勤报表
 */
apf_require_class("Bll_BLL");
apf_require_class("Bll_Attendance_UserInfo");
apf_require_class("Tools_OAUrls");
apf_require_class('Bll_Attendance_AccessDayRecord');
apf_require_class('Bll_Attendance_DailyLeave');
class Bll_Attendance_Report extends BLL{

    private $bll_user;
    const START_KQ_RECORD = '20120701';//打卡记录开始时间，小于这个的不算

    //当月结算日
    private $curr_end_day;
    private $_data_type=1;
    public function set_data_type($data){
        $this->_data_type = $data;
    }
    public function build_report_data_new($params,$work_days,$session_userid){
        $result = array();
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_user->enable_cache = false;

        $users = $this->get_all_user_info($params, $session_userid);
        if (empty($users['uids'])) {
            return $result;
        }
        $tmp = array();
        $temp = array();
        $start = $params['select_year'].$params['select_month'].'01';
        $end = $params['select_year'].$params['select_month'].date('t',mktime(0,0,0,$params['select_month'],1,$params['select_year']));
        $ck_bll = new Bll_Attendance_DailyLeave();
        $Tools = Tools_Tools::get_instance();
        foreach ($users['data'] as $key => $val) {
            $tmp= $ck_bll->get_report_data($val['user_id'],$start,$end);
            $temp[$key] = array_merge($val,$tmp);
        }
        $users['data'] = $temp;
        $result = $users;
        $uids = array_keys($result['data']);
        $surplus_days = $ck_bll->get_surplus_annual_days($uids);
        foreach($surplus_days as $uid=>$val){
            $result['data'][$uid]['nianjia'] = $val;
        }
        return $result;
    }

    public function build_report_detail_data_new($params){
        $result = array();
        if (empty($params['user_id'])) {
            return $result;
        }
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_user->enable_cache = false;
        $Tools = Tools_Tools::get_instance();
        $ck_bll = new Bll_Attendance_DailyLeave();
        for ($i=$params['offset'];$i<$params['offset']+$params['row'];$i++){
            $start_unix = mktime(0,0,1,date('m')-$i,1,date('Y'));
            $days = date('t',$start_unix);
            $end_unix = mktime(23,59,59,date('m')-$i,$days,date('Y'));

            $ym = date('Y年m月',$start_unix);
            $start = date('Ymd',$start_unix);
            $end = date('Ymd',$end_unix);
            $result[$ym] =$ck_bll->get_report_data($params['user_id'],$start,$end);
        }
        return $result;
    }
    public function build_report_execl_data_new($uid,$params){
        $result = array();
        if (empty($uid)) {
            return $result;
        }
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_user->enable_cache = false;

        $users = $this->bll_user->get_user_by_ids($uid);
        $userinfo = $users[0];
        //都是从当前月份开始
        $Tools = Tools_Tools::get_instance();
        $ck_bll = new Bll_Attendance_DailyLeave();
        $year = $params['select_year'];
        $month = $params['select_month'];
        $start_unix = mktime(0,0,1,$month,1,$year);
        $days = date('t',$start_unix);
        $end_unix = mktime(23,59,59,$month,$days,$year);
        $start = date('Ymd',$start_unix);
        $end = date('Ymd',$end_unix);
        return $ck_bll->get_report_data($uid,$start,$end);
    }
    /**
     * 封装考勤报表数据
     * @param array 查询参数
     * @param int 指定月工作天数
     * @param int 登陆用户的ID
     * @return array
     * **/
    public function build_report_data($params,$work_days,$session_userid) {
        $datatype = APF::get_instance()->get_config('datatype');
        if($this->_data_type){
            if($_GET['cls'] || $datatype){
                return $this->build_report_data_new($params,$work_days,$session_userid);
            }
        }
        $result = array();
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_user->enable_cache = false;
        $this->curr_end_day = 28;
        $users = $this->get_all_user_info($params, $session_userid);
        if (empty($users['uids'])) {
            return $result;
        }
        $leave_record = $this->get_format_leave_record($users['uids'], $params);

        //与请假信息组合数据
        $tmp = array();
        $temp = array();
        $start = $params['select_year'].$params['select_month'].'01';
        $end = $params['select_year'].$params['select_month'].date('t',mktime(0,0,0,$params['select_month'],1,$params['select_year']));
        $cau_end = $params['select_year'].$params['select_month'].$this->curr_end_day;//28日结算

        $Tools = Tools_Tools::get_instance();
        foreach ($users['data'] as $key => $val) {
            if(date('Ymd',strtotime($end)) < date('Ymd',strtotime($val['enter_date']))){
                unset($users['data'][$key]);
                continue;
            }
            $tmp = $leave_record[$val['user_id']];
            $cd_kg = $this->get_be_late_day($val, $start, $end);//迟到或者旷工数据;
            if(!empty($cd_kg['kglist'])){
                $cd_kg['kglist'] = array_unique($cd_kg['kglist']);
            }
            //当月离职数据处理,离职日期到当月最后天数计算
            $curr_not_work=$Tools->current_month_not_work_day($params['select_year'],$params['select_month'],date('Ymd',strtotime($val['enter_date'])),date('Ymd',strtotime($val['dimission_date'])));
            //入职当月，且是28号之后入职的
            $select_month_endday = date('Ymd',mktime(0,0,0,$params['select_month']+1,0,$params['select_year']));
            $select_month_28 = date('Ymd',mktime(0,0,0,$params['select_month'],$this->curr_end_day,$params['select_year']));
            $user_enter_date = date('Ymd',strtotime($val['enter_date']));

            $tmp['oa_kg'] = $cd_kg['kg'];
            $tmp['oa_cd'] = $cd_kg['cd'];
            $tmp['oa_prexj'] = $this->get_unsettled_day_new($val,$start,$end);//'上月未扣
            $tmp['oa_url'] = Tools_OAUrls::get_instance()->build_user_detail_url($val['user_id'],$params['select_year'].$params['select_month'],$this->_data_type);
            //入职当月，且是28号之后入职的
            if( $user_enter_date <= $select_month_endday && $user_enter_date > $select_month_28){
                $tmp['work_act_day'] = 0;
                $tmp['rice_day'] = 0;
                $tmp['meal'] = 0;
                $tmp['work_act_not_day'] =  $work_days;
            }else{
                $tmp['work_act_day'] = $work_days - $tmp['not_act_work']-$cd_kg['kg'] - $curr_not_work+$tmp['oa_sp_passed'];
                $tmp['work_act_day'] = $tmp['work_act_day'] <0 ? 0 : $tmp['work_act_day'];
                $tmp['rice_day'] = $work_days - $tmp['not_meal_work']-count($cd_kg['kglist']) - $curr_not_work;
                $tmp['rice_day'] = $tmp['rice_day'] <0 ? 0 : $tmp['rice_day'];
                $tmp['meal'] = $this->get_meal($work_days, $tmp['rice_day']);
                $tmp['work_act_not_day'] = $Tools->current_month_not_work_day($params['select_year'],$params['select_month'],date('Ymd',strtotime($val['enter_date'])),date('Ymd',strtotime($val['dimission_date'])));
            }
            $temp[$key] = array_merge($val,$tmp);
        }
        $users['data'] = $temp;
        $result = $users;
        return $result;
    }

    /**
     * 封装考勤明细数据
     * @param array 查询参数
     * @param int 指定月工作天数
     * @param int 登陆用户的ID
     * @return array
     * **/
    public function build_report_detail_data($params) {
        $datatype = APF::get_instance()->get_config('datatype');
        if($this->_data_type){
            if($_GET['cls'] || $datatype){
                return $this->build_report_detail_data_new($params);
            }
        }
        $result = array();
        $this->curr_end_day = 28;
        if (empty($params['user_id'])) {
            return $result;
        }
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_user->enable_cache = false;

        $users = $this->bll_user->get_user_by_ids($params['user_id']);
        $userinfo = $users[0];
        //都是从当前月份开始
        $Tools = Tools_Tools::get_instance();

        for ($i=$params['offset'];$i<$params['offset']+$params['row'];$i++){
            $start_unix = mktime(0,0,1,date('m')-$i,1,date('Y'));
            $days = date('t',$start_unix);
            $end_unix = mktime(23,59,59,date('m')-$i,$days,date('Y'));

            $ym = date('Y年m月',$start_unix);

            $to_params = array(
                'select_year'=>date('Y',$start_unix),
                'select_month'=>date('m',$start_unix),
                    );
            //每月应工作的天数
            $work_days = $Tools->current_month_work_day($to_params['select_year'],$to_params['select_month']);
            $leave_record = $this->get_format_leave_record(array($params['user_id']), $to_params);

            $tmp = array();
            if(empty($leave_record)){
                $tmp['not_act_work'] = 0;
                //不计算餐补的天数
                $tmp['not_meal_work'] = 0;
                $tmp['oa_gc'] = 0;
                $tmp['oa_gc_passed'] = 0;
                $tmp['oa_cc'] = 0;
                $tmp['oa_cc_passed'] = 0;
                $tmp['oa_sj'] = 0;
                $tmp['oa_sj_passed'] = 0;
                $tmp['oa_bj'] = 0;
                $tmp['oa_bj_passed'] = 0;
                $tmp['oa_nj'] = 0;
                $tmp['oa_nj_passed'] = 0;
                $tmp['oa_oj'] = 0;
                $tmp['oa_oj_passed'] = 0;
                $tmp['other_more'] = array();
            }else{
                $tmp = $leave_record[$params['user_id']];
            }

            $start = date('Ymd',$start_unix);
            $end = date('Ymd',$end_unix);

            $cd_kg = $this->get_be_late_day($userinfo, $start, $end);//迟到或者旷工数据;

            if(!empty($cd_kg['kglist'])){
                $cd_kg['kglist'] = array_unique($cd_kg['kglist']);
            }
            $tmp['oa_kg'] = $cd_kg['kg'];
            $tmp['oa_cd'] = $cd_kg['cd'];
            $tmp['oa_prexj'] = $this->get_unsettled_day_new($userinfo,$start,$end);
            //入职当月，且是28号之后入职的
            $select_month_endday = date('Ymd',mktime(0,0,0,$to_params['select_month']+1,0,$to_params['select_year']));
            $select_month_28 = date('Ymd',mktime(0,0,0,$to_params['select_month'],$this->curr_end_day,$to_params['select_year']));
            $user_enter_date = date('Ymd',strtotime($userinfo['enter_date']));
            $tmp['work_att_day'] = $work_days;
            //入职当月，且是28号之后入职的
            if( $user_enter_date <= $select_month_endday && $user_enter_date > $select_month_28){
                $tmp['work_act_day'] = 0;
                $tmp['rice_day'] = 0;
                $tmp['meal'] = 0;
                $tmp['work_act_not_day'] =  $work_days;
            }else{
                //未全勤
                $tmp['work_act_not_day'] = $Tools->current_month_not_work_day($to_params['select_year'],$to_params['select_month'],date('Ymd',strtotime($userinfo['enter_date'])),date('Ymd',strtotime($userinfo['dimission_date'])));
                $tmp['work_act_day'] = $work_days - $tmp['not_act_work']-$cd_kg['kg'] - $tmp['work_act_not_day']+$tmp['oa_sp_passed'];
                $tmp['work_act_day'] = $tmp['work_act_day'] <0 ? 0 : $tmp['work_act_day'];
                $tmp['rice_day'] = $work_days - $tmp['not_meal_work']-count($cd_kg['kglist']) - $tmp['work_act_not_day'];
                $tmp['rice_day'] = $tmp['rice_day'] <0 ? 0 : $tmp['rice_day'];
                $tmp['meal'] = $this->get_meal($work_days, $tmp['rice_day']);
            }
            $tmp['oa_url'] = Tools_OAUrls::get_instance()->build_user_detail_url($params['user_id'],$to_params['select_year'].$to_params['select_month'],$this->_data_type);

            $temp[$ym] = $tmp;
        }
        $result = $temp;
        return $result;
    }
    /**
     * TODO 需要和这个函数合并build_report_detail_data()
     * 考勤报告中导出数据....
     * @param unknown_type $user
     * @param array $params array(select_year=> select_month=>)
     */
    public function build_report_execl_data($uid,$params){
        $datatype = APF::get_instance()->get_config('datatype');
        if($this->_data_type){
            if($_GET['cls'] || $datatype){
                return $this->build_report_execl_data_new($uid,$params);
            }
        }
        $this->curr_end_day = 28;
        $result = array();

        if (empty($uid)) {
            return $result;
        }
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_user->enable_cache = false;

        $users = $this->bll_user->get_user_by_ids($uid);
        $userinfo = $users[0];
        //都是从当前月份开始
        $Tools = Tools_Tools::get_instance();
        $year = $params['select_year'];
        $month = $params['select_month'];
        $start_unix = mktime(0,0,1,$month,1,$year);
        $days = date('t',$start_unix);
        $end_unix = mktime(23,59,59,$month,$days,$year);

        $to_params = array(
                'select_year'=>$year,
                'select_month'=>$month,
        );
        //每月应工作的天数
        $work_days = $Tools->current_month_work_day($to_params['select_year'],$to_params['select_month']);
        //echo date('Y-m-d H:i',$start_unix)."==".date('Y-m-d H:i',$end_unix)."=--work_days".$work_days."---".$i."----=".$days."-----------<br>";
        $leave_record = $this->get_format_leave_record(array($uid), $to_params);

        $tmp = array();
        if(empty($leave_record)){
            $tmp['not_act_work'] = 0;
            //不计算餐补的天数
            $tmp['not_meal_work'] = 0;
            $tmp['oa_gc'] = 0;
            $tmp['oa_gc_passed'] = 0;
            $tmp['oa_cc'] = 0;
            $tmp['oa_cc_passed'] = 0;
            $tmp['oa_sj'] = 0;
            $tmp['oa_sj_passed'] = 0;
            $tmp['oa_bj'] = 0;
            $tmp['oa_bj_passed'] = 0;
            $tmp['oa_nj'] = 0;
            $tmp['oa_nj_passed'] = 0;
            $tmp['oa_oj'] = 0;
            $tmp['oa_oj_passed'] = 0;
            $tmp['other_more'] = array();
        }else{
            $tmp = $leave_record[$uid];
        }
        $start = date('Ymd',$start_unix);
        $end = date('Ymd',$end_unix);
        //当月离职数据处理,离职日期到当月最后天数计算
        $curr_not_work=$Tools->current_month_not_work_day($to_params['select_year'],$to_params['select_month'],date('Ymd',strtotime($userinfo['enter_date'])),date('Ymd',strtotime($userinfo['dimission_date'])));
        $cd_kg = $this->get_be_late_day($userinfo, $start, $end);//迟到或者旷工数据;
        if(!empty($cd_kg['kglist'])){//上下午的去排重
            $cd_kg['kglist'] = array_unique($cd_kg['kglist']);
        }
        $tmp['oa_kg'] = $cd_kg['kg'];
        $tmp['oa_cd'] = $cd_kg['cd'];
        $tmp['oa_prexj'] = $this->get_unsettled_day_new($userinfo,$start,$end);//'上月未扣
        $tmp['work_att_day'] = $work_days;
        //入职当月，且是28号之后入职的
        $select_month_endday = date('Ymd',mktime(0,0,0,$to_params['select_month']+1,0,$to_params['select_year']));
        $select_month_28 = date('Ymd',mktime(0,0,0,$to_params['select_month'],$this->curr_end_day,$to_params['select_year']));
        $user_enter_date = date('Ymd',strtotime($userinfo['enter_date']));
        //入职当月，且是28号之后入职的
        if( $user_enter_date <= $select_month_endday && $user_enter_date > $select_month_28){
            $tmp['work_act_day'] = 0;
            $tmp['rice_day'] = 0;
            $tmp['meal'] = 0;
            $tmp['work_act_not_day'] =  $work_days;
        }else{
            $tmp['work_act_day'] = $work_days - $tmp['not_act_work']-$cd_kg['kg'] - $curr_not_work+$tmp['oa_sp_passed'];
            $tmp['work_act_day'] = $tmp['work_act_day'] <0 ? 0 : $tmp['work_act_day'];
            $tmp['rice_day'] = $work_days - $tmp['not_meal_work']-count($cd_kg['kglist']) - $curr_not_work;
            $tmp['rice_day'] = $tmp['rice_day'] <0 ? 0 : $tmp['rice_day'];
            $tmp['meal'] = $this->get_meal($work_days, $tmp['rice_day']);
            $tmp['work_act_not_day'] = $Tools->current_month_not_work_day($params['select_year'],$params['select_month'],date('Ymd',strtotime($userinfo['enter_date'])),date('Ymd',strtotime($userinfo['dimission_date'])));
        }
        $tmp['oa_url'] = Tools_OAUrls::get_instance()->build_user_detail_url($uid,$to_params['select_year'].$to_params['select_month'],$this->_data_type);

        return $tmp;
    }

    /**
     * 获取所有员工信息，包含当月离职信息
     * @param array $params
     * @return array
     * **/
    public function get_all_user_info($params = array(),$superiro_id) {
        $result = array();
        $where = array();
        //组装查询条件
        $where['superior_id'] = $superiro_id;
        if(!empty($params['level'])) {
            $where['level'] = $params['level'];
        }
        if(!empty($params['select_level'])) {
            $where['level'] = $params['select_level'];
        }
        if(!empty($params['select_city'])) {
            $where['city_name'] = $params['select_city'];
        }
        if(!empty($params['select_department'])) {
            $where['department_id'] = $params['select_department'];
        }
        if(!empty($params['keywords'])) {
            $where['name'] = $params['keywords'];
        }

        if(!empty($params['select_year'])){
            $where['select_year'] = $params['select_year'];
        }
        if(!empty($params['select_month'])){
            $where['select_month'] = $params['select_month'];
        }

        $page = (int)$params['p'];
        $page = empty($page) ? 1:$page;

        $order = "user_id asc";
        $offset = $params['offset'];

        //对hr做特殊处理
        $role = $this->bll_user->get_user_role($superiro_id);
        if($role[$superiro_id]['role_id'] == 3){
            unset($where['level']);
            $result = $this->bll_user->search_subordinate_userinfo_hr($where,$order,$params['row'],$offset);
            $result['is_hr'] = 1;
        }
        else{
            $result = $this->bll_user->search_subordinate_userinfo($where,$order,$params['row'],$offset);
        }
        //组装所有用户 ids
        if ($result) {
            $uids = array();
            foreach ($result['data'] as $val) {
                $uids[] = $val['user_id'];
            }
            $result['uids'] = $uids;
        }
        return $result;
    }

     /**
     * 获取按用户ID组装好的请假记录数据
     * @param int | array() $uids
     * @param int $start 开始时间
     * @param int $end 结束时间点
     * @param array
     * **/
    public function get_format_leave_record($uids,$params) {
        $reuslt = array();
        $tmp = $this->get_all_leave_record($uids, $params);
        if (empty($tmp)) {
            return $reuslt;
        }

        $type = array('公出'=>'oa_gc','出差'=>'oa_cc','事假'=>'oa_sj','病假'=>'oa_bj','年假'=>'oa_nj','其他假'=>'oa_oj','产检'=>'oa_cj','陪产假'=>'oa_pcj','婚假'=>'oa_hj','丧假'=>'oa_sangj');
        $type_key = array('公出','出差','事假','病假','年假','其他假');
        $other_type = array('丧假','产假','产前假','产检','婚假','请假','陪产假');
        $not_consider = array('迟到','缺勤','早退');
        $status = array('已批准'=>'passed','未审核'=>'wait');
        $status_key = array('已批准','未审核');
        $have_half_day = array();
        $end_days = array();
        foreach ($tmp as $key => $val) {
            //没有取到与用户ID的数据，初始化数据
            if(!isset($result[$val['user_id']])) {
                //没有实际出勤的天数
                $result[$val['user_id']]['not_act_work'] = 0;
                //不计算餐补的天数
                $result[$val['user_id']]['not_meal_work'] = 0;
                $result[$val['user_id']]['oa_gc'] = 0;
                $result[$val['user_id']]['oa_gc_passed'] = 0;
                $result[$val['user_id']]['oa_cc'] = 0;
                $result[$val['user_id']]['oa_cc_passed'] = 0;
                $result[$val['user_id']]['oa_sj'] = 0;
                $result[$val['user_id']]['oa_sj_passed'] = 0;
                $result[$val['user_id']]['oa_bj'] = 0;
                $result[$val['user_id']]['oa_bj_passed'] = 0;
                $result[$val['user_id']]['oa_nj'] = 0;
                $result[$val['user_id']]['oa_nj_passed'] = 0;
                $result[$val['user_id']]['oa_oj'] = 0;
                $result[$val['user_id']]['oa_oj_passed'] = 0;
                $result[$val['user_id']]['oa_sp_passed'] = 0;
                $result[$val['user_id']]['other_more'] = array();
                $have_half_day[$val['user_id']] = array();
                $end_days[$val['user_id']] = array();
            }
            if (!in_array($val['status'],$status_key)) {
                continue;
            }
            $end_time = substr($val['end_time'], 0, 10);
            //函数处理跨月的情况,now 为本月，last为上月数据
            if ($type[$val['type']] != 'oa_gc') {
                $real_leave_time = $this->cutting_leave_time($val['start_time'], $val['end_time'], $val['leave_time'],$params);
                $val['leave_time'] = $real_leave_time['now'];
            }
            if($type[$val['type']] == 'oa_hj' || $type[$val['type']] == 'oa_pcj' || $type[$val['type']] == 'oa_sangj'){
                if ($status[$val['status']] == 'passed') {
                    $result[$val['user_id']]['oa_sp_passed'] +=$val['leave_time'];
                }
            }

            //无需考虑计算的类型
            if (in_array($val['type'],$not_consider)) {
                continue;
            } elseif (in_array($val['type'], $type_key)) {
                if(!in_array($val['end_time'], $end_days[$val['user_id']])) {
                    $result[$val['user_id']][$type[$val['type']]] += $val['leave_time'];
                    if ($status[$val['status']] == 'passed') {
                        $passed_key = $type[$val['type']].'_passed';
                        $result[$val['user_id']][$passed_key] += $val['leave_time'];
                    }
                }
            } else {
                //计算其它假,不做同类型假的汇总,减少临时数组
                if(!in_array($val['end_time'], $end_days[$val['user_id']])) {
                    $result[$val['user_id']]['oa_oj'] += $val['leave_time'];
                    if($val['leave_time']){
                        $result[$val['user_id']]['other_more'][] = array('type' => $val['type'],'leave_time' => $val['leave_time']);
                    }
                    if ($status[$val['status']] == 'passed') {
                        $result[$val['user_id']]['oa_oj_passed'] += $val['leave_time'];
                    }
                }
            }

            //唉！数组的内存分配机制，这个开始让我担心内存溢出问题了,
            if (!in_array($val['end_time'], $end_days[$val['user_id']])) {
                //年假，公出，出差,产检不扣工资的啦
                if($type[$val['type']] != 'oa_gc' && $type[$val['type']] != 'oa_cc' && $type[$val['type']] != 'oa_nj' && $type[$val['type']] != 'oa_cj') {
                    $result[$val['user_id']]['not_act_work'] += $val['leave_time'];
                }
            }
            if(!in_array($end_time, $have_half_day[$val['user_id']])) {
                //计算不算餐补的天数 请半天假，扣一天餐补，同一天请两个半天，只扣一天
                if($type[$val['type']] != 'oa_gc' && $type[$val['type']] != 'oa_cc') {
                    $result[$val['user_id']]['not_meal_work'] += round($val['leave_time']);
                }
            }
            $have_half_day[$val['user_id']][] = $end_time;
            $end_days[$val['user_id']][] = $val['end_time'];
        }
        return $result;
    }

    /**
     * 获取用户的请假记录
     * @param int | array() $uids
     * @param int $start 开始时间
     * @param int $end 结束时间点
     * @param array
     * **/
    public function get_all_leave_record($uids,$params) {
        $result = array();
        if(empty($uids)) {
            return $result;
        }
        $year = date('Y');
        $month = date('m');
        if(!empty($params['select_year'])){
            $year = $params['select_year'];
        }
        if(!empty($params['select_month'])){
            $month = $params['select_month'];
        }
        $querytime = $this->get_time($year.$month.'01');

        apf_require_class('Bll_Attendance_LeaveRecord');
        $leave_bll = new Bll_Attendance_LeaveRecord();
        $result = $leave_bll->get_leave_record_by_between($uids,$querytime['start'],$querytime['end']);

        return $result;
    }

    //get one month AccessDayRecord
    public function get_work_day_status($uid,$start,$end){
        $time = strtotime($start);
        $month = intval(date('m',$time));
        $bll = new Bll_Attendance_AccessDayRecord();
        $tmp_access_data = $bll->get_record_list($uid,$start,$end);
        foreach($tmp_access_data as $val){//日期为主键,这里可能是周6打卡
            $status=$bll->check_day_work_status($val['access_date'],$val['start_work'],$val['end_work']);
            $val['cd']=0;
            $val['kg']=0;
            if($status[0]==1){
                $val['cd']=1;
            }
            if($status[0]==3){
                $val['kg']+=0.5;
            }
            if($status[1]==4){
                $val['kg']+=0.5;
            }
            $val['status'] = $status;
            $access_data[$val['access_date']] = $val;
        }
        return $access_data;

    }
    public function get_unsettled_day_new($userinfo,$start,$end){

        apf_require_class('Bll_Attendance_LeaveRecord');
        $leave_bll = new Bll_Attendance_LeaveRecord();
        $time = strtotime($start);
        $month = intval(date('m',$time));
        $year = substr($start,0,4);
        $uid= $userinfo['user_id'];

        if($userinfo['city_name'] != '上海'){
            return array();
        }
        //销售部
        $sale_department = APF::get_instance()->get_config('sale_department');

        $prev_unix = mktime(23,59,59,$month-1,28,$year);//上月28号之前的时间缀标记
        $prev_date = $year.date('m',$prev_unix).'28';

        $checkday = 28;//TODO
        $tmp_unix_start = mktime(0,0,0,$month-1,$checkday+1,$year);
        $tmp_unix_end =mktime(23,59,59,$month,$checkday,$year);

        apf_require_class('Tools_Tools');
        $tools = Tools_Tools::get_instance();
        //need know 29 30 31 's card log //只有取上月的工作日
        $prev_month_work = $tools->actual_work_day_by_month_list(date('Ym',$prev_unix));
        //这里只算上个月的
        $prev_one = mktime(23,59,59,$month-1,1,$year);
        $prev_days = date('t',$prev_one);
        $prev_end = mktime(23,59,59,$month-1,$prev_days,$year);

        $end_date = intval(str_replace('-','',substr($userinfo['enter_date'],0,10)));//入职时间
        $dimission_date = intval(str_replace('-','',substr($userinfo['dimission_date'],0,10)));//入职时间

        $tmp_access_data = $this->get_work_day_status($uid,date('Ymd',$prev_one),date('Ymd',$prev_end));
        $access_data = array();
        if(!empty($tmp_access_data)){
            foreach($tmp_access_data as $k=> $val){
                $day = substr($k,6);
                if($k=='20120929'){//这里写死了...
                    if($val['end_work'] && $val['end_work']>=1600){
                        if($val['kg']>0){
                            $val['kg'] -= 0.5;
                        }else{
                            $val['kg'] = 0;
                        }
                        $val['status'][1]=6;
                    }
                }
                if(intval($day) >28){
                    if ($userinfo['user_type'] == 1 && $k >= $userinfo['user_type_date']) {
                        if($val['status'][0]==3 && $val['status'][1]==4){
                            $val['kg'] = 1;
                        }else{
                            $val['kg'] = 0;
                            $val['status']=array(5,6);
                        }
                    }
                    $access_data[$k] = $val;
                }
            }
        }

        //缺勤临时数据 主要为了解决 29,30,31没有打卡记录，也没有请假的
        $day_que_list = array();
        $end_need_list = array();

        foreach($prev_month_work as $val){//$k = 20120801
            if($val > date('Ymd',$prev_unix)){//29,30,31
                if($end_date > $prev_date && $val >=$end_date && substr($val,4,2)==substr($end_date,4,2)){
                    $end_need_list[]=$val;
                }
                if($val <=$end_date){//小于入职时间的不算
                    continue;
                }
                if($dimission_date && $val >$dimission_date){//大于离职时间的不算
                    continue;
                }

                if(!empty($access_data[$val])){
                    if(!empty($access_data[$val]['kg'])){
                        $day_que_list[$val] +=$access_data[$val]['kg'];
                    }
                }else{
                    if($val >=self::START_KQ_RECORD){
                        $day_que_list[$val] +=1;
                    }
                }
            }
        }

        $rs = $leave_bll->get_leave_record_use_apply_time($uid,$year,$month);
        //查看这个员工是否在请长假
        apf_require_class('Bll_Attendance_UserExt');
        $user = new Bll_Attendance_UserExt();
        $leave_status = $user->get_last_leave_status($uid);
        if($leave_status){
            $leave_bll = new Bll_Attendance_LeaveRecord();
            $rs[]= $leave_bll->get_leave_by_id($leave_status);
        }

        $wk=array();
        if(empty($rs)) {//当前时间段是否有产前假和产假
            //$wk = $this->get_maternity_days($uid,$tmp_unix_start,$tmp_unix_end);
            //可能是旷工
        }else{
            $record_type_add = array(
                //'早退','公出','出差'
            );

            foreach ($rs as $v){
                if($v['type']=='早退'){//跟钱无关的假不计算
                    continue;
                }
                $strday = str_replace('-', '', substr($v['start_time'],0,10));
                /*if($v['type']=='缺勤' && in_array($v['leave_reason'],array('忘记带卡','忘记刷卡','地铁故障','其它'))){
                    if(isset($day_que_list[$strday])){//如果这里没有分上下午处理
                        unset($day_que_list[$strday]);
                    }
                    continue;
                }*/

                if($v['type']=='缺勤'){
                    if(isset($day_que_list[$strday])){
                        if($v['leave_reason'] == '忘记带卡'){
                            $day_que_list[$strday] -=1;
                            unset($day_que_list[$strday]);
                        }else{//如果请了两次 同一天缺勤只算一次
                            $day_que_list[$strday] -=0.5;
                            if(empty($day_que_list[$strday])){
                                unset($day_que_list[$strday]);
                            }
                        }
                        if ($userinfo['user_type'] == 1 && $strday >= $userinfo['user_type_date']) {
                            unset($day_que_list[$strday]);
                        }
                    }
                    continue;
                }
                /*if($v['type']=='迟到' && $dayno > $checkday){
                    if(isset($day_que_list[$strday])){//
                        $day_que_list[$strday] -=0.5;
                    }
                    continue;
                }*/
                if(!isset($wk[$v['type']])){//初始每个假都是0
                    $wk[$v['type']] = 0;
                }
                if($v['type']=='产检'){//每月一天的产检
                    if(!isset($wk[$v['type']])){
                        $wk[$v['type']] = 1;
                    }
                    continue;
                }

                //格式化时间,方便计算
                $leave_start = strtotime($v['start_time']);
                $leave_end = strtotime($v['end_time']);
                $start_h = intval(substr($v['start_time'],11,2));
                $end_h = intval(substr($v['end_time'],11,2));

                $start_day = date('Y-m-d',$leave_start);
                $leave_no_hour = strtotime($start_day);
                $bigdate = date('Ymd',$leave_start);
                $cur_access = $access_data[$bigdate];

                /*
                if(!$end_h){////如果是迟到，早退,缺勤是没小时的
                    $end_h = 18;
                    $leave_end +=18*3600;
                }*/
                if($v['type']=='迟到'){
                    $start_h = 9;
                    $end_h = 18;
                    $leave_start +=9*3600;
                    $leave_end +=18*3600;
                    //加入忘带卡特殊情况
                    if($val['leave_reason']=='忘记带卡'){
                        $start_h = 9;
                        $end_h = 18;
                        $leave_start =$leave_no_hour+9*3600;
                        $leave_end =$leave_no_hour+18*3600;
                    }
                }elseif($v['type']=='缺勤'){

                    if($v['leave_reason']=='忘记带卡'){
                        $start_h = 9;
                        $end_h = 18;
                        $leave_start =$leave_no_hour+9*3600;
                        $leave_end =$leave_no_hour+18*3600;
                    }else{//check hour
                        if(!$end_h){//zhe li shi xia wu que qing
                            if($cur_access['status'][0]==3 && $cur_access['status'][1]==4){
                                $start_h = 9;
                                $end_h = 14;
                                $leave_start = $leave_no_hour+9*3600;
                                $leave_end = $leave_no_hour+14*3600;
                            }else{
                                if($cur_access['status'][0]==3){

                                    $start_h = 9;
                                    $end_h = 14;
                                    $leave_start =$leave_no_hour+9*3600;
                                    $leave_end =$leave_no_hour+14*3600;
                                    //echo $start_day."bbbbbbbb".date('Y-m-d H:i',$leave_start)."bb".date('Y-m-d H:i',$leave_end)."bbbb<br>";
                                }elseif($cur_access['status'][1]==4){
                                    $start_h = 14;
                                    $end_h = 18;
                                    $leave_start =$leave_no_hour+14*3600;
                                    $leave_end =$leave_no_hour+18*3600;
                                }
                            }
                        }else{
                            if($start_h == 9){
                                $start_h = 9;
                                $end_h = 14;
                                //echo $date."==".date('Y-m-d H:i',$leave_start)."=".date('Y-m-d H:i',$leave_end)."==000000000mmmmmmmmmmmmmm<br>";
                                $leave_start =$leave_no_hour+9*3600;
                                $leave_end =$leave_no_hour+14*3600;
                            }else{
                                $start_h = 14;
                                $end_h = 18;
                                //$leave_start +=14*3600;
                                $leave_start =$leave_no_hour+14*3600;
                                $leave_end =$leave_no_hour+18*3600;
                            }
                        }
                    }
                }elseif($v['type']=='公出'){
                    if(isset($cur_access['start_work'])){
                        $st_h=substr($cur_access['start_work'],0,-2);
                        if($cur_access['status'][0] !=5){
                            $start_h=min($st_h,$start_h);
                            //$start_h = $st_h;
                            $end_h = 14;
                        }
                        if($cur_access['status'][1] !=6){
                            $start_h = 14;
                            //$end_h = 18;
                        }
                        $leave_start = $leave_no_hour+$start_h*3600;
                        $leave_end = $leave_no_hour+$end_h*3600;

                    }else{//公出没有打卡记录
                        $leave_start = $leave_no_hour+$start_h*3600;
                        $leave_end = $leave_no_hour+$end_h*3600;
                    }
                }

                if($end_h==9){// 时间2012-08-24 09:00 到2012-08-30 09:00 结束时间修正为 2012-08-30 18:00
                    $leave_end -= 54000;//回退12小时到前一天18:00
                    $end_h = 18;
                }
                if($start_h==18){//从第二天900开始算
                    $leave_start +=54000;
                    $start_h=9;
                }
                $apply_day = substr($v['apply_time'],8,2);
                $start_day = substr($v['start_time'],8,2);
                $end_day = substr($v['end_time'],8,2);
                $apply_date = str_replace('-','',substr($v['apply_time'],0,10));
                $j=1;
                for ($i=$leave_start;$i<=$leave_end;$i+=86400){//
                    $date = date('Ymd',$i);
                    if(!in_array($date,$prev_month_work)) {//不是工作日不处理
                        continue;
                    }
                    if($date > date('Ymd',$prev_end)){//大于上月最后一天的不算
                        continue;
                    }
                    if($apply_date <=$prev_date){
                        if(isset($day_que_list[$date])){
                            unset($day_que_list[$date]);
                        }
                        continue;
                    }
                    //这里取了两个月的请假记录,只算申请时间在29,30,31, 同时请假时间在29,30,31
                    $day_no = intval(substr($date,6,2));//$month $prev_days

                    if($day_no < $this->curr_end_day+1 && $apply_day <$this->curr_end_day+1){
                        continue;
                    }
                    //这里计算补假后应该使用的天数
                    $acr=1;
                    if($i==$leave_end){//最后一天
                        if($end_h==14){
                            $acr = 0.5;
                        }else{
                            $acr = 1;
                        }
                    }else{//第一天
                        $acr = 1;
                        if($j==1){
                            if($start_h==9 && $end_h==18){
                                $acr = 1;
                            }else{
                                $acr = 0.5;
                            }
                        }
                    }

                    if(!empty($day_que_list[$date])){//如果这里没有分上下午处理
                        unset($day_que_list[$date]);
                    }
                    if($v['type']=='迟到' || $v['type']=='公出'){
                        continue;
                    }
                    if($date <=$prev_date ){//月耒请的假 0328
                        if($v['type']=='事假'){//1-28号不计事假
                            continue;
                        }
                        $wk[$v['type']] +=$acr;
                    }elseif($date <$start){//时间必须小于当前7.1号 29/30/31处理
                        if($v['type']=='事假' || $v['type']=='病假' || $v['type']=='产前假' ){//这时期不计年假
                            $wk[$v['type']] -=$acr;
                        }
                    }
                    $j++;
                }
            }//end foreach
        }

        if(!empty($day_que_list)){
            $wk['缺勤'] = !isset($wk['缺勤']) ? 0 : $wk['缺勤'];
            foreach ($day_que_list as $val){
                $wk['缺勤'] -=$val;
            }
        }else{
            $wk['缺勤'] =0;
        }

        if(!empty($sale_department[$userinfo['department_id']])){
            $wk['缺勤'] =0;
        }
        if($end_need_list){//这里是为了计算当月28号后入职
            $ek = count($end_need_list);
            foreach($wk as $k=>$val){
                if($k=='缺勤'){
                    $wk[$k]=0;
                }else{
                    $wk[$k] +=abs($val);
                }
                $ek -= abs($val);
            }
            $wk[$end_date] =$ek;
        }

        if(!empty($wk)){//去天数为0
            foreach ($wk as $type=> $val){
                if(!$val){
                    unset($wk[$type]);
                }
            }
        }
        return $wk;
    }


    /**
     * 获取迟到天数
     * @param int | array() $uids
     * @param int $start 开始时间 20120601
     * @param int $end 结束时间点 20120801
     * **/
    public function get_be_late_day($userinfo,$start,$end) {
        apf_require_class('Tools_Tools');
        $tools = Tools_Tools::get_instance();
        $uid = $userinfo['user_id'];
        $end_date = intval(str_replace('-','',substr($userinfo['enter_date'],0,10)));//入职时间
        $dimission_date = intval(str_replace('-','',substr($userinfo['dimission_date'],0,10)));//入职时间
        $bll = new Bll_Attendance_AccessDayRecord();
        $tmp_access_data = $bll->get_record_list($uid,$start,$end);
        $res = array();
        $access_data=array();
        foreach($tmp_access_data as $val){//日期为主键,这里可能是周6打卡
            $status=$bll->check_day_work_status($val['access_date'],$val['start_work'],$val['end_work']);
            $val['cd']=0;
            $val['kg']=0;

            if($status[0]==1){
                $val['cd']=1;
            }
            if($status[0]==3){
                $val['kg']+=0.5;
            }
            if($status[1]==4){
                $val['kg']+=0.5;
            }
            $val['status'] = $status;
            $access_data[$val['access_date']] = $val;
        }

        $worklist = array(
                'cd'=>0,//迟到
                'kg'=>0,//旷工
                'kglist'=>array(),
        );

        $worklist_new = array(
                'uid'=>$userinfo['user_id'],
                'city_name'=>$userinfo['city_name'],
                'department_name'=>$userinfo['department_name'],
        );
        $sale_department = APF::get_instance()->get_config('sale_department');
        $year_month = substr($start, 0,6);
        $month_work = $tools->actual_work_day_by_month_list($year_month);//每个月需要上班的日期

        $last_access = array();//最后需要确认的一个月状态
        foreach($month_work as $day){
            if($day <self::START_KQ_RECORD){
                continue;
            }
            if($day>=date('Ymd')){//大于今天的
                continue;
            }
            if($end_date >= $day){//如果员工是查询月入职的,应该入职当天也不算
                continue;
            }
            if($dimission_date && $day >= $dimission_date){//如果员工查询月一天离职
                continue;
            }
            $day_no = intval(substr($day,6,2));
            if( $day_no > $this->curr_end_day){
                continue;
            }

            if(!isset($access_data[$day])){//需要上班的而没有打卡记录
                $last_access[$day]['access_date']=$day;
                $last_access[$day]['cd']=0;
                $last_access[$day]['kg']=1;
                $last_access[$day]['status'] = array(3,4);
                $worklist['kglist'][]=$day;//状态都是分上下午计算,所以这里存两次
                $worklist['kglist'][]=$day;
            }else{
                $last_access[$day] = $access_data[$day];

                if ($userinfo['user_type'] == 1 && $day >= $userinfo['user_type_date']) {
                    $last_access[$day]['cd']=0;
                    $last_access[$day]['kg']=0;
                    $last_access[$day]['status'] = array(5,6);
                }else{
                    if($access_data[$day]['status'][0]==3 ){
                        $worklist['kglist'][]=$day;
                    }
                    if($access_data[$day]['status'][1]==4){
                        $worklist['kglist'][]=$day;
                    }
                }
            }
        }
//print_r($last_access);
//print_r($worklist);
        //kglist不去重,如果是分上下午旷工，作两次处理
        $year = substr($start,0,4);//只处理当年当月份
        $month = substr($start,4,2);
        //这里其实是多取了一次
        $leave_data = $this->get_all_leave_record(array($uid),array('select_year'=>$year,'select_month'=>$month));

        //查看这个员工是否在请长假
        apf_require_class('Bll_Attendance_UserExt');
        $user = new Bll_Attendance_UserExt();
        $leave_status = $user->get_last_leave_status($uid);
        if($leave_status){
            $leave_bll = new Bll_Attendance_LeaveRecord();
            $tmp_leave_data = $leave_bll->get_leave_by_id($leave_status);
            foreach ($leave_data as $k=> $val){
                if($val['id']==$tmp_leave_data['id']){
                    unset($leave_data[$k]);break;
                }
            }
            $leave_data[] = $tmp_leave_data;
        }
//print_r($leave_data);
        if(!empty($leave_data)){
            foreach($leave_data as $val){
                $leave_start = strtotime($val['start_time']);
                $leave_end = strtotime($val['end_time']);
                $start_day = date('Y-m-d',$leave_start);
                $leave_no_hour = strtotime($start_day);
                $bigdate = date('Ymd',$leave_start);
                $cur_access = $last_access[$bigdate];
                //echo date('Y-m-d H:i:s',$leave_no_hour)."xxxxx<br>";
                //如果是迟到，早退,缺勤是没小时的
                $start_h = intval(substr($val['start_time'],11,2));
                $end_h = intval(substr($val['end_time'],11,2));

                if($val['type']=='迟到'){
                    //echo $bi

                    $start_h = 9;
                    $end_h = 14;
                    if($val['leave_reason']=='忘记带卡'){
                        $start_h = 9;
                        $end_h = 18;
                        $leave_start =$leave_no_hour+9*3600;
                        $leave_end =$leave_no_hour+18*3600;
                    }
                    $leave_start =$leave_no_hour+$start_h*3600;
                    $leave_end =$leave_no_hour+$end_h*3600;
                }elseif($val['type']=='缺勤'){
                    if($val['leave_reason']=='忘记带卡'){
                            $start_h = 9;
                            $end_h = 18;
                            $leave_start =$leave_no_hour+9*3600;
                            $leave_end =$leave_no_hour+18*3600;
                    }else{//check hour
                        //var_dump($cur_access);
                        if(!$end_h){//zhe li shi xia wu que qing
                            if($cur_access['status'][0]==3 && $cur_access['status'][1]==4){
                                $start_h = 9;
                                $end_h = 14;
                                $leave_start = $leave_no_hour+9*3600;
                                $leave_end = $leave_no_hour+14*3600;
                            }else{
                                if($cur_access['status'][0]==3){

                                    $start_h = 9;
                                    $end_h = 14;
                                    $leave_start =$leave_no_hour+9*3600;
                                    $leave_end =$leave_no_hour+14*3600;
                                    //echo $start_day."bbbbbbbb".date('Y-m-d H:i',$leave_start)."bb".date('Y-m-d H:i',$leave_end)."bbbb<br>";
                                }elseif($cur_access['status'][1]==4){
                                    $start_h = 14;
                                    $end_h = 18;
                                    $leave_start =$leave_no_hour+14*3600;
                                    $leave_end =$leave_no_hour+18*3600;
                                }
                            }
                        }else{
                            if($start_h == 9){
                                $start_h = 9;
                                $end_h = 14;
                                //echo $date."==".date('Y-m-d H:i',$leave_start)."=".date('Y-m-d H:i',$leave_end)."==000000000mmmmmmmmmmmmmm<br>";
                                $leave_start =$leave_no_hour+9*3600;
                                $leave_end =$leave_no_hour+14*3600;
                            }else{
                                $start_h = 14;
                                $end_h = 18;
                                //$leave_start +=14*3600;
                                $leave_start =$leave_no_hour+14*3600;
                                $leave_end =$leave_no_hour+18*3600;
                            }
                        }
                    }
                }elseif($val['type']=='公出'){
                    if(isset($cur_access['start_work'])){
                        //var_dump($cur_access);var_dump($end_h);
                        $st_h=substr($cur_access['start_work'],0,-2);
                        if($cur_access['status'][0] !=5){
                            $start_h=min($st_h,$start_h);
                            //$start_h = $st_h;
                            $end_h = 14;
                        }
                        if($cur_access['status'][1] !=6){
                            $start_h = 14;
                            $end_h = intval(substr($val['end_time'],11,2));
                            //$end_h = 18;
                        }
                        $leave_start = $leave_no_hour+$start_h*3600;
                        $leave_end = $leave_no_hour+$end_h*3600;

                    }else{//公出没有打卡记录
                        $leave_start = $leave_no_hour+$start_h*3600;
                        $leave_end = $leave_no_hour+$end_h*3600;
                    }
                }
                if($end_h==9){// 时间2012-08-24 09:00 到2012-08-30 09:00 结束时间修正为 2012-08-30 18:00
                    $leave_end -= 54000;//回退12小时到前一天18:00
                    $end_h = 18;
                }
                if($start_h==18){//从第二天900开始算
                    $leave_start +=54000;
                    $start_h=9;
                }
                //print_r($val);
                //echo "==".date('Y-m-d H:i',$leave_start)."=".date('Y-m-d H:i',$leave_end)."==mmmmmmmmmmmmmm<br>";
                for ($i=$leave_start;$i<=$leave_end;$i+=86400){//从请假跨度中,计算是否有消除不正常考勤状态
                    $date = date('Ymd',$i);
                    if(!in_array($date,$month_work)) {//必须工作日
                        continue;
                    }

                    $need_d = intval(substr($date,6,2));
                    if(isset($last_access[$date])){//请假日期和工作日有关系
                        if ($userinfo['user_type'] == 1 && $day >= $userinfo['user_type_date']) {
                            $last_access[$date]['kg'] =0;
                            for($j=0;$j<2;$j++){
                                $key=array_search($date,$worklist['kglist']);
                                if($key!==false){
                                    unset($worklist['kglist'][$key]);
                                }
                            }
                            break;
                        }
                        if($last_access[$date]['status'][0]==1){
                            $last_access[$date]['cd'] -=1;
                        }elseif($last_access[$date]['status'][0]==3){//需要一个请假来消除

                            //if($val['type']!='迟到'){//
                                $needtime_s = mktime(9,0,0,$month,$need_d,$year);
                                $needtime_e = mktime(14,0,0,$month,$need_d,$year);
                                if($needtime_s >=$leave_start && $needtime_e<=$leave_end){
                                    //echo $date."xxxx********".$last_access[$date]['kg']."*******xxx=====".date('Y-m-d H:i',$needtime_s)."======".date('Y-m-d H:i',$needtime_e)."==".date('Y-m-d H:i',$leave_start)."=".date('Y-m-d H:i',$leave_end)."==<br>";
                                    $last_access[$date]['kg'] -=0.5;

                                    $key=array_search($date,$worklist['kglist']);
                                    if($key!==false){
                                        unset($worklist['kglist'][$key]);
                                    }
                                }
                               // print_r($worklist);

                            //}
                        }
                        if($last_access[$date]['status'][1]==4){
                            //echo $date."xxxx********".$last_access[$date]['kg']."*******xxx=====".date('Y-m-d H:i',$needtime_s)."======".date('Y-m-d H:i',$needtime_e)."==".date('Y-m-d H:i',$leave_start)."=".date('Y-m-d H:i',$leave_end)."==<br>";
                            if($val['leave_reason']=='地铁故障'){
                                continue;
                            }
                            if($val['type']!='迟到' || $val['leave_reason']=='忘记带卡' || $val['leave_reason']=='忘记刷卡'){
                                $needtime_s = mktime(14,0,0,$month,$need_d,$year);
                                $needtime_e = mktime(18,0,0,$month,$need_d,$year);

                                if($needtime_s >=$leave_start && $needtime_e<=$leave_end){
                                    $last_access[$date]['kg'] -=0.5;
                                    $key=array_search($date,$worklist['kglist']);
                                    if($key!==false){
                                        unset($worklist['kglist'][$key]);
                                    }
                                }
                            }
                        }
                    }//end if
                }
            }//end foreach leave_data
        }
//print_r($worklist);
        if(!empty($last_access)){
            foreach($last_access as $val){
                if($val['cd']>0){
                    $worklist['cd'] += $val['cd'];
                }
                if($val['kg']>0){
                    $worklist['kg'] += $val['kg'];
                }
            }
        }

        if($worklist_new['city_name'] !='上海' || strpos($worklist_new['department_name'], '销售')){
            if(!empty($sale_department[$userinfo['department_id']])){
                $worklist['cd'] =0;
                $worklist['kg'] =0;
                $worklist['kglist'] =array();
            }
        }
        return $worklist;
    }

     /**
     * 切割时间点，主要针对跨月的数据
     * @param string $start
     * @param string $end
     * @param array(select_year select_month)
     * @return array
     * **/
    public function cutting_leave_time($start,$end,$leave_time,$params) {
        $result = array('last' => 0,'now' => 0);
        if (empty($start) || empty($end)) {
            return $result;
        }
        //如果是跨整月请假
        $yearmonth = $params['select_year'].$params['select_month'].'01';
        if(str_replace('-','',substr($start,0,10)) <=$yearmonth){
            $start = $params['select_year'].'-'.$params['select_month'].'-01 09:00';
        }
        $cur_endday = $params['select_year'].$params['select_month'].date('t',strtotime($end));
        if(str_replace('-','',substr($end,0,10)) >=$cur_endday){
            $end = $params['select_year'].'-'.$params['select_month'].'-'.date('t',strtotime($end)).' 18:00';
        }
        $start_tamp = strtotime($start);
        $end_tamp = strtotime($end);
        if(date('m',$start_tamp) < $params['select_month'] && date('m',$end_tamp)<$params['select_month']){
            return $result;
        }
        if (date('m',$start_tamp) == date('m',$end_tamp)) {
            $leave_time = Tools_Tools::get_instance()->get_many_day($start, $end);
            return array('last' => 0,'now' => $leave_time);
        }
        //本月第一天时间戳
        $e_middle_tamp = date('Y-m',$end_tamp).'-01 09:00';
        //上一月最后一天时间戳
        $s_middle_tamp = date('Y-m',$start_tamp).'-'.date('t',$start_tamp).' 18:00';

        apf_require_class('Tools_Tools');
        $last = Tools_Tools::get_instance()->get_many_day($start, $s_middle_tamp);
        $now = Tools_Tools::get_instance()->get_many_day($e_middle_tamp, $end);

        if (date('m',$start_tamp) < date('m',$end_tamp) && date('m',$start_tamp)==$params['select_month']) {//特殊处理跨月份的
            return array('last' => $now,'now' => $last);
        }else{
            return array('last' => $last,'now' => $now);
        }
    }

    /**
     * 时间格式处理
     */
    public function get_time($time = 0) {
        $result = array('start'=>0,'end'=>'0');
        if(empty($time)) {
            return $result;
        }
        //echo $time;
        $time = strtotime($time);
        $month = date('m',$time);
        $start = date('Y-m-d',mktime(0,0,0,$month,1,date('Y',$time)));//本月开始
        $end = date('Y-m-d',mktime(0,0,0,$month+1,1,date('Y',$time)));//结束日期
        $end = date('Y',$time).'-'.date('m',$time).'-'.$this->curr_end_day;
        $result['start'] = $start;
        $result['end'] = $end." 23";
        return $result;
    }

    /**
     * 餐补计算
     * @param unknown_type $att_day 应出勤天数
     * @param unknown_type $act_day 实出勤天数 餐补计算
     */
    public function get_meal($att_day,$act_day) {
        $meal = APF::get_instance()->get_config('oa_meal');
        $meal = round($meal/$att_day*$act_day);
        return $meal;
    }

    /**
     * @return Dao_Attendance_AccessDayRecord
     */
    private function get_app_dao(){
        $dao = $this->get_dao('Dao_Attendance_AccessDayRecord');
        return $dao;
    }
    public function set_bll_user($bll){
        $this->bll_user = $bll;
        $this->bll_user->enable_cache = false;
    }
}